﻿using AutoMapper;
using MediatR;
using MyMES.Domain.yjb;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.settingCommand;
using MyMES.Writer.API.FileNlog;

namespace MyMES.Writer.API.Application.CommendHandlers.settingCommand
{
    public class UpdateProcessInfoCommandHandlers : IRequestHandler<UpdateProcessInfoCommand, APIResult<ProcessInfo>>
    {
        private readonly IBaseRepository<ProcessInfo> accreop;
        private readonly IMapper mapper;
        private readonly Nolgs nolgs;

        public UpdateProcessInfoCommandHandlers(IBaseRepository<ProcessInfo> accreop, IMapper mapper,Nolgs nolgs)
        {
            this.accreop = accreop;
            this.mapper = mapper;
            this.nolgs = nolgs;
        }
        /// <summary>
        /// 生产-修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<ProcessInfo>> Handle(UpdateProcessInfoCommand request, CancellationToken cancellationToken)
        {
            var startTime= DateTime.Now;
            try
            {
                APIResult<ProcessInfo> result = new APIResult<ProcessInfo>();
                result.Code = APIEnum.修改成功;
                result.Msg = "修改成功";
                var process = mapper.Map<ProcessInfo>(request);
                string sql = " update ProcessInfo set  processCode=@processCode, processName=@processName, isEnabled=@isEnabled, processDescription=@processDescription, remarks=@remarks, operationStepInfoId=@operationStepInfoId,isDelete=@isDelete   where  processInfoId=@processInfoId;";
                await accreop.Update(process, sql);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                var endTime = System.DateTime.Now;

                var durationInDays = (endTime - startTime).Days;
                nolgs.GetNlogs(
                    request.ProcessName ?? "Unknow", "工序修改", "False", durationInDays, "异常信息"
                    );
            }
        }
    }
}
